#! /bin/sh

# export CI_COMMIT_REF_NAME='installbts'  &&  sh buildpvch5.sh

echo "start building.."
pwd
git status
echo "CI_COMMIT_REF_NAME = $CI_COMMIT_REF_NAME"
if [ $CI_COMMIT_REF_NAME = master ] ; then pkgname=$(date +%Y%m%d-%H%M%S); else pkgname=$(date +%Y%m%d-%H%M%S)-dev; fi
echo "<!-- ${pkgname} -->" >> src/pages/document.ejs
env PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true yarn

if [ $CI_COMMIT_REF_NAME = master ] ; 
then AISLE_ORIGIN=https://aisle.pano.video AWX_JOB_ID=''; NODE_ENV='production';  echo "" > .env; 
elif [ $CI_COMMIT_REF_NAME = dev ] ; 
then AISLE_ORIGIN=https://aisle.dev.pano.video AWX_JOB_ID=26530 ; NODE_ENV='development'; echo "COMPRESS=none" > .env; 
else AISLE_ORIGIN=https://aisle.bts.pano.video AWX_JOB_ID=26531; NODE_ENV='development';  echo "COMPRESS=none" > .env;
fi
echo output AISLE_ORIGIN=$AISLE_ORIGIN AWX_JOB_ID=$AWX_JOB_ID

# build
env AISLE_ORIGIN=$AISLE_ORIGIN NODE_ENV=$NODE_ENV yarn run build

# upload version + pkg
prodName='pvch5'
cd dist
rm -f ../${prodName}.*.tar
tar -zcvf ../${prodName}.${pkgname}.tar ./
echo output ${pkgname}.tar
curl -u admin:P@ile0822 --upload-file ../${prodName}.${pkgname}.tar  http://repo.pano.video/repository/raw-hosted/pvch5/${pkgname}.tar
echo ${pkgname} > version
curl -u admin:P@ile0822 --upload-file ./version  http://repo.pano.video/repository/raw-hosted/pvch5/version

# awx job trigger
if [ x$AWX_JOB_ID = x ];
then 
echo ignore master branch
else
curl -XPOST http://static:L8gWmcD1AL@awx.pano.video/api/v2/job_templates/$AWX_JOB_ID/launch/
fi

cd ..
sleep 5
sh cinotify.sh ${prodName} ${pkgname}
